package com.sinosun.autotestmachine.tools.proxy;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.sinosun.autotestmachine.App;
import com.sinosun.autotestmachine.tools.ValueInfo;
import com.sinosun.autotestmachine.tools.ValueType;
import com.sinosun.autotestmachine.tools.Collector.Cache;
import com.sinosun.autotestmachine.tools.Collector.MsgSendCollector;
import com.sinosun.autotestmachine.tools.parser.ContentMaker;
import com.sinosun.autotestmachine.tools.parser.NodeDetail;
import com.sinosun.autotestmachine.tools.parser.ParamKey;
import com.sinosun.autotestmachine.tools.parser.TopicInformation;
import com.sinosun.autotestmachine.tools.proxy.callback.CallbackWrapper;
import com.sinosun.autotestmachine.tools.utils.Utils;
import com.sinosun.mstplib.MstpClient;
import com.sinosun.mstplib.MstpException;
import com.sinosun.mstplib.message.Message;
import com.sinosun.mstplib.topic.ReplyType;
import com.sinosun.mstplib.topic.Topic;
import com.sinosun.mstplib.topic.TopicMemberInfo;
import com.sinosun.mstplib.topic.TopicNodeInfo;
import com.sinosun.mstplib.topic.TopicReplyInfo;
import com.sinosun.mstplib.topic.TopicStatus;

import android.text.TextUtils;
import android.util.Log;

public class TopicModule {

	protected static int createTopic(MstpClient mstpClient, String paramJson, String cacheParam) {
		String name = new String();
		String content = new String();
		List<String> caches = new ArrayList<String>();
		try {
			JSONObject jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.NAME)) {
				name = jo.getString(ParamKey.NAME);
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
			if (jo.has(ParamKey.CONTENT)) {
				content = jo.getString(ParamKey.CONTENT);
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
			
			if (cacheParam != null) {
				JSONObject joCaches = new JSONObject(cacheParam);
				if (joCaches.has(ParamKey.TOPIC_ID)) {			//获取所有缓存信息
					String cacheTag = joCaches.getString(ParamKey.TOPIC_ID);
					caches.add(cacheTag);
				} else {
					return ErrorCode.ERR_PARAM_ERROR;
				}
			}
			
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		
		try {
			Log.e("XXX", "createTopic name=" + name + ",content=" + content);
			String topicId = mstpClient.getTopicManager().createTopicWithName(name, content);
			Log.e("XXX", "createTopic topicId=" + topicId);
			for (int i = 0; i < caches.size(); i++) {
				TopicInformation item = new TopicInformation();
				Topic topic = new Topic();
				topic.setTopicId(topicId);
				topic.setTopicName(name);
				topic.setContent(content);
				item.setTopic(topic);
				Cache.allTopicInfo.put(caches.get(i), item);
			}
			
			if (!TextUtils.isEmpty(topicId)) {
				Topic compareTopic = new Topic();
				compareTopic.setTopicId(topicId);
				compareTopic.setTopicName(name);
				compareTopic.setContent(content);
				if (hasTopicFromMyTopics(mstpClient, compareTopic)) {
					return ErrorCode.SUCCESS;
				}
			}
		} catch (MstpException e) {
			return e.getErrorCode();
		}
		return ErrorCode.ERR_UNEXPECT_RESULT;
	}
	
	private static boolean hasTopicFromMyTopics(MstpClient mstpClient, Topic topic) {
		try {
			List<Topic> myTopics = mstpClient.getTopicManager().queryAllTopics();
			if (myTopics == null) {
				return false;
			}
			for (Topic ele : myTopics) {
				if (ele.getTopicId().equals(topic.getTopicId()) && ele.getTopicName().equals(topic.getTopicName()) && ele.getContent().equals(topic.getContent())) {
					return true;
				}
			}
		} catch (MstpException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	private static boolean hasTopicByTopicId(MstpClient mstpClient, Topic topic) {
		try {
			Topic targetTopic = mstpClient.getTopicManager().queryTopicWithTopicId(topic.getTopicId());
			if (targetTopic == null) {
				return false;
			}
			
			if (targetTopic.getTopicId().equals(topic.getTopicId()) 
					&& targetTopic.getTopicName().equals(topic.getTopicName()) 
					&& targetTopic.getContent().equals(topic.getContent())) {
				return true;
			}
		} catch (MstpException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	protected static int updateTopic(MstpClient mstpClient, String paramJson) {
		String name = new String();
		String content = new String();
		String topicId = new String();
		try {
			JSONObject jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.NAME)) {
				name = jo.getString(ParamKey.NAME);
				Log.e("XXX", "updateTopic name=" + name);
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
			if (jo.has(ParamKey.CONTENT)) {
				content = jo.getString(ParamKey.CONTENT);
				Log.e("XXX", "updateTopic content=" + content);
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
			if (jo.has(ParamKey.TOPIC_ID)) {
				topicId = getTopicId(jo);
				Log.e("XXX", "updateTopic topicId=" + topicId);
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
			
			try {
				Log.e("XXX", "updateTopic topicId=" + topicId + ", name=" + name + ", content=" + content);
				int ret = mstpClient.getTopicManager().updateTopicWithTopicId(topicId, name, content);
				if (ret != ErrorCode.SUCCESS) {
					return ret;
				}
				
				Topic compareTopic = new Topic();
				compareTopic.setTopicId(topicId);
				compareTopic.setTopicName(name);
				compareTopic.setContent(content);
				Log.e("XXX", "compareTopic topicId=" + compareTopic.getTopicId() + ", name=" + compareTopic.getTopicName() + ", content=" + compareTopic.getContent());
				if (hasTopicByTopicId(mstpClient, compareTopic)) {
					return ErrorCode.SUCCESS;
				}
				
			} catch (MstpException e) {
				e.printStackTrace();
				return e.getErrorCode();
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		return ErrorCode.ERR_UNEXPECT_RESULT;
	}
	
	private static boolean getTopicStatusWithType(MstpClient mstpClient, String topicId, int type) {
		return getTopicStatusWithType(mstpClient, topicId, type, null);
	}
	
	private static boolean getTopicStatusWithType(MstpClient mstpClient, String topicId, int type, String mstpId) {
		try {
			Topic topic = mstpClient.getTopicManager().queryTopicWithTopicId(topicId);
			Log.e("XXX", "getTopicStatusWithType mstpId -> " + topic.getCreator());
			Log.e("XXX", "getTopicStatusWithType owner -> " + topic.isOwner());
			Log.e("XXX", "getTopicStatusWithType type -> " + type);
			if (topic != null) {
				if(type == 0 && topic.getTopicStatus() == TopicStatus.CLOSED){		//是否关闭
					return true;
				} else if (type == 1 && topic.getTopicStatus() == TopicStatus.NORMAL) {		//是否打开
					return true;
				} else if (type == 2 && topic.isTop()) {		//是否置顶
					return true;
				} else if (type == 3 && mstpClient.getMstpId().equals(topic.getCreator())) {		//当前对象是否是拥有者且是创建者
					return true;
				} else if (type == 4 && !mstpClient.getMstpId().equals(topic.getCreator())) {		//当前对象是否不是拥有者且不是创建者
					return true;
				} else if (type == 5 && !mstpId.equals(topic.getCreator())) {		//指定对象是否不是拥有者且不是创建者
					return true;
				} else if (type == 6 && mstpId.equals(topic.getCreator())) {			//指定对象是否是拥有者且是创建者
					return true;
				} else {
					return false;
				}
			} else {
				return false;
			}
		} catch (MstpException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	protected static int function4Topic(MstpClient mstpClient, String paramJson, int type) {
		String topicId = new String();
		try {
			JSONObject jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.TOPIC_ID)) {
				topicId = getTopicId(jo);
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
			try {
				int ret = ErrorCode.ERR_UNEXPECT_RESULT;
				if (type == 0) {		//关闭话题
					Log.e("XXX", "function4Topic close");
					ret = mstpClient.getTopicManager().closeTopicWithTopicId(topicId);
					if (ret != ErrorCode.SUCCESS) {
						return ret;
					}
					if (!getTopicStatusWithType(mstpClient, topicId, 0)) {
						ret = ErrorCode.ERR_UNEXPECT_RESULT;
					}
					
				} else if (type == 1){		//打开话题
					Log.e("XXX", "function4Topic open");
					ret = mstpClient.getTopicManager().openTopicWithTopicId(topicId);
					if (ret != ErrorCode.SUCCESS) {
						return ret;
					}
					if (!getTopicStatusWithType(mstpClient, topicId, 1)) {
						ret = ErrorCode.ERR_UNEXPECT_RESULT;
					}
					
				} else if (type == 2) {		//放弃话题
					Log.e("XXX", "function4Topic quit");
					ret = mstpClient.getTopicManager().quitTopicWithTopicId(topicId);
					if (ret != ErrorCode.SUCCESS) {
						return ret;
					}
					List<TopicMemberInfo> allMember = mstpClient.getTopicManager().queryTopicMembersWithTopicId(topicId);
					for (TopicMemberInfo ele : allMember) {
						if (ele.getMemberMstpId().equals(mstpClient.getMstpId())) {
							ret = ErrorCode.ERR_UNEXPECT_RESULT;
							break;
						}
					}
					
				} else if (type == 3) {		//删除投票
					Log.e("XXX", "function4Topic delete");
					ret = mstpClient.getTopicManager().deleteTopicWithTopicId(topicId);
					if (ret != ErrorCode.SUCCESS) {
						return ret;
					}
					List<Topic> allTopics = mstpClient.getTopicManager().queryAllTopics();
					for (Topic ele : allTopics) {
						if (ele.getTopicId().equals(topicId)) {		//包含被删除的 topicId 直接退出循环，删除失败
							ret = ErrorCode.ERR_UNEXPECT_RESULT;
							break;
						}
					}
					
				} else if (type == 4) {		//置顶话题
					Log.e("XXX", "function4Topic stick");
					ret = mstpClient.getTopicManager().setTopWithTopicId(topicId);
					if (ret != ErrorCode.SUCCESS) {
						return ret;
					}
					if (!getTopicStatusWithType(mstpClient, topicId, 2)) {
						ret = ErrorCode.ERR_UNEXPECT_RESULT;
					}
					
				} else if (type == 5) {		//转移话题
					Log.e("XXX", "function4Topic transfer");
					String targetMstpId = new String();
					if (jo.has(ParamKey.TARGET_MSTPID)) {
						targetMstpId = jo.getString(ParamKey.TARGET_MSTPID);
						if (TextUtils.isEmpty(targetMstpId)) {
							
						} else {
							ValueInfo value = Utils.getUserRealInfo(targetMstpId);
							targetMstpId = value.getValueContent();
							if (value.getValueType() == ValueType.INDEX) {		// value 数据内容符合匹配规则，从内存中取出对应的 mstpId
								targetMstpId = App.getTestConfig().getUsers().get(value.getValueContent());
							}
						}
					} else {
						return ErrorCode.ERR_PARAM_ERROR;
					}
					Log.e("XXX", "function4Topic param topicId=" + topicId + ", targetMstpId=" + targetMstpId);
					ret =  mstpClient.getTopicManager().transferTopicWithTopicId(topicId, targetMstpId);
					if (ret != ErrorCode.SUCCESS) {
						return ret;
					}
					if (mstpClient.getMstpId().equals(targetMstpId) && 					//转移与被转移者是一个人
							!getTopicStatusWithType(mstpClient, topicId, 3)) {
						ret = ErrorCode.ERR_UNEXPECT_RESULT;
					} else if (!mstpClient.getMstpId().equals(targetMstpId) && 			//当前对象不是拥有者，被转移对象是拥有者
							!((getTopicStatusWithType(mstpClient, topicId, 4) && getTopicStatusWithType(mstpClient, topicId, 6, targetMstpId)))) {
						ret = ErrorCode.ERR_UNEXPECT_RESULT;
					}
				} else if (type == 6) {
					ret = mstpClient.getTopicManager().unsetTopWithTopicId(topicId);
					if (ret != ErrorCode.SUCCESS) {
						return ret;
					}
					if (getTopicStatusWithType(mstpClient, topicId, 2)) {		//依旧处于置顶状态
						ret = ErrorCode.ERR_UNEXPECT_RESULT;
					}
				} else if (type == 7) {
					mstpClient.getTopicManager().queryTopicWithTopicId(topicId);
					return ErrorCode.SUCCESS;
				} else if (type == 8) {
					mstpClient.getTopicManager().queryTopicMembersWithTopicId(topicId);
					return ErrorCode.SUCCESS;
				} else if (type == 9) {
					mstpClient.getTopicManager().queryAllNodesWithTopicId(topicId);
					return ErrorCode.SUCCESS;
				}
				return ret;
			} catch (MstpException e) {
				e.printStackTrace();
				return e.getErrorCode();
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
	}
	
	protected static int memberUpdate(MstpClient mstpClient, String paramJson, int type) {
		String topicId = new String();
		List<String> memberList = new ArrayList<String>();
		try {
			JSONObject jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.TOPIC_ID)) {
				topicId = getTopicId(jo);
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
			
			JSONArray memberListJa = null;
			if (jo.has(ParamKey.MEMBERS)) {
				memberListJa = jo.getJSONArray(ParamKey.MEMBERS);
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
			
			if (memberListJa == null) {
				return ErrorCode.ERR_PARAM_ERROR; 
			}
			
			for (int i = 0; i < memberListJa.length(); i++) {
				String tmp = memberListJa.getString(i);
				ValueInfo vi = Utils.getUserRealInfo(tmp);
				String userMstpId = new String();
				if (vi.getValueType() == ValueType.INDEX) {
					userMstpId = App.getTestConfig().getUsers().get(vi.getValueContent());
				} else {
					userMstpId = tmp;
				}
				memberList.add(userMstpId);
			}
			
			try {
				int ret = ErrorCode.ERR_UNEXPECT_RESULT;
				if (type == 0) {		//邀请成员
					ret = mstpClient.getTopicManager().inviteMembersWithTopicId(topicId, memberList);
				} else if (type == 1) {		//移除成员
					ret = mstpClient.getTopicManager().removeMembersWithTopicId(topicId, memberList);
				}
				
				if (ret != ErrorCode.SUCCESS) {
					return ret;
				}
				List<TopicMemberInfo> allMember = mstpClient.getTopicManager().queryTopicMembersWithTopicId(topicId);
				List<String> allMstpId = new ArrayList<String>();
				if (allMember == null) {
					return ErrorCode.ERR_UNEXPECT_RESULT;
				} 
				for (TopicMemberInfo ele : allMember) {
					allMstpId.add(ele.getMemberMstpId());
				}
				
				if (type == 0 && allMstpId.containsAll(memberList)) {
					ret = ErrorCode.SUCCESS;
				} else if (type == 1){
					ret = ErrorCode.SUCCESS;
					for (String eleStr : memberList) {
						if (allMstpId.contains(eleStr)) {
							ret = ErrorCode.ERR_UNEXPECT_RESULT;
							break;
						}
					}
				} else {
					ret = ErrorCode.ERR_UNEXPECT_RESULT;
				}
				
				return ret;
			} catch (MstpException e) {
				e.printStackTrace();
				return e.getErrorCode();
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
	}
	
	/**
	 * 检索出 topicId
	 * @param jo
	 * @return
	 * @throws JSONException
	 */
	private static String getTopicId(JSONObject jo) throws JSONException {
		if (jo.isNull(ParamKey.TOPIC_ID)) {
			return null;
		}
		Topic topic = getTopic(jo);
		if (topic != null) {
			return topic.getTopicId();
		}
		return jo.getString(ParamKey.TOPIC_ID);
//		ValueInfo vi = Utils.getTopicRealInfo(topicId);
//		if (vi.getValueType() == ValueType.INDEX) {
//			TopicInformation get = Cache.allTopicInfo.get(topicId);
//			if (get != null) {
//				topicId = get.getTopic().getTopicId();
//			} else {
//				topicId = null;
//			}
//		} else {
//			topicId = vi.getValueContent();
//		}
//		return topicId;
	}
	
	/**
	 * 从缓存中获取 topic 信息
	 * @param jo
	 * @return
	 * @throws JSONException
	 */
	private static Topic getTopic(JSONObject jo) throws JSONException {
		String topicId = jo.getString(ParamKey.TOPIC_ID);
		ValueInfo vi = Utils.getTopicRealInfo(topicId);
		if (vi.getValueType() == ValueType.INDEX) {
			TopicInformation get = Cache.allTopicInfo.get(topicId);
			if (get != null) {
				return get.getTopic();
			} else {
				return null;
			}
		}
		return null;
	}
	
	protected static int sendMessageAndVerify(MstpClient mstpClient, String paramJson, String cacheParam) {
		List<String> params = new ArrayList<String>();				//保存所有的消息内容
		List<String> cacheParams = new ArrayList<String>();			//缓存对应数据
		try {
			JSONObject jo = new JSONObject(paramJson);
			if(jo.has(ParamKey.MESSAGR_CONTENT_ID)) {
				JSONArray jsonArr = jo.getJSONArray(ParamKey.MESSAGR_CONTENT_ID);
				for (int i = 0; i < jsonArr.length(); i++) {
					ValueInfo value = Utils.getMsgRealInfo(jsonArr.getString(i));
					if (value.getValueType() == ValueType.INDEX) {
						String tmp = App.getTestConfig().getMsgContent().get(value.getValueContent());
						params.add(tmp);
					} else {
						params.add(jsonArr.getString(i));
					}
				}
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
			
			if (cacheParam != null) {
				JSONObject cacheJo = new JSONObject(cacheParam);
				if (cacheJo.has(ParamKey.SEND_MESSAGE_ID)) {
					JSONArray cacheArr = cacheJo.getJSONArray(ParamKey.SEND_MESSAGE_ID);
					for (int i = 0; i < cacheArr.length(); i++) {
						cacheParams.add(cacheArr.getString(i));
					}
				} else {
					return ErrorCode.ERR_PARAM_ERROR;
				}
			}
		} catch (JSONException e1) {
			e1.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		
		if (params.size() <= 0 || cacheParams.size() <= 0) {
			return ErrorCode.ERR_PARAM_ERROR;
		}
		
		try {
			MsgSendCollector collector = new MsgSendCollector();
			for (String content : params) {	//遍历发送所有的数据
				Log.e("XXX", "content:" + content);
				Message m = ContentMaker.makeMessage(content, mstpClient.getMstpId());
				String messageId = mstpClient.sendMessage(m);
				m.setMsgId(messageId);
				collector.addMessageID(m);
			}
			
			CallbackWrapper.makeMessageSendCallback(collector);		//设置监听
			collector.waitResult();		//阻塞等待处理结果
			CallbackWrapper.clearMessageSendCallback();
			if (collector.getResult() == ErrorCode.SUCCESS) {
				for (int i = 0; i < cacheParams.size(); i++) {
					Cache.messageSendCache.put(cacheParams.get(i), collector.getMessageIDs().get(i));
				}
			}
			return collector.getResult();
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		} catch (MstpException e) {
			e.printStackTrace();
			return e.getErrorCode();
		} catch (InterruptedException e) {
			e.printStackTrace();
			return ErrorCode.ERR_UNEXPECT_RESULT;
		}
	}
	
	public static int queryTopicWithTopicId(MstpClient mstpClient, String paramJson, String cachJson) {
		String topicId = new String();
		String cacheIndex = new String();
		try {
			JSONObject jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.TOPIC_ID)) {
				topicId = getTopicId(jo);
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
			
			if (cachJson != null) {
				JSONObject cacheJo = new JSONObject(cachJson);
				if (cacheJo.has(ParamKey.TOPIC_INFO)) {
					cacheIndex = cacheJo.getString(ParamKey.TOPIC_INFO);
				} else {
					return ErrorCode.ERR_PARAM_ERROR;
				}
			}
			
			Topic topic = mstpClient.getTopicManager().queryTopicWithTopicId(topicId);
			Topic tagTopic = getTopic(jo);
			if (topic != null && tagTopic != null && topic.getContent().equals(tagTopic.getContent())&&
					topic.getTopicId().equals(tagTopic.getTopicId()) && topic.getTopicName().equals(tagTopic.getTopicName())) {
				return ErrorCode.SUCCESS;
			}
			if (topic != null) {
				TopicInformation item = new TopicInformation();		//将本次查询到的 topic 信息存入缓存
				item.setTopic(topic);
				Cache.allTopicInfo.put(cacheIndex, item);
			}
			return ErrorCode.ERR_UNEXPECT_RESULT;
		} catch(JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		} catch (MstpException e) {
			e.printStackTrace();
			return e.getErrorCode();
		}
	}
	
	public static int commentAndReplyVerfiry(MstpClient mstpClient, String paramJson) {
		String topicId = new String();
		List<Message> params = new ArrayList<Message>();		//保存所有的消息内容
		try {
			JSONObject jo = new JSONObject(paramJson);
			if (jo.has(ParamKey.TOPIC_ID)) {
				topicId = getTopicId(jo);
				Log.e("XXX", "commentAndReplyVerfiry topicId=" + topicId);
			}
				
			if(jo.has(ParamKey.VERIFY_MSGID)) {
				JSONArray jsonArr = jo.getJSONArray(ParamKey.VERIFY_MSGID);
				for (int i = 0; i < jsonArr.length(); i++) {
					if (Cache.messageSendCache.get(jsonArr.getString(i)) != null) {
						params.add(Cache.messageSendCache.get(jsonArr.getString(i)));
						Log.e("XXX", "msgId=" + Cache.messageSendCache.get(jsonArr.getString(i)).getMsgId());
					} else {
						return ErrorCode.ERR_PARAM_ERROR;
					}
				}
			} else {
				return ErrorCode.ERR_PARAM_ERROR;
			}
			
			try {
				List<TopicNodeInfo> topicNodes = mstpClient.getTopicManager().queryAllNodesWithTopicId(topicId);
				Log.e("XXX", "commentAndReplyVerfiry topicNodes.size=" + topicNodes.size());
				int tag = 0;
				for (Message item : params) {
					for (TopicNodeInfo ele : topicNodes) {
						Log.e("XXX", "nodeId -> " + ele.getNodeId());
						Log.e("XXX", "msgId -> " + item.getMsgId());
						if (ele.getNodeId().equals(item.getMsgId())) {
							tag++;
							break;
						}
					}
				}
				Log.e("XXX", "commentAndReplyVerfiry tag=" + tag);
				Log.e("XXX", "commentAndReplyVerfiry params.size=" + params.size());
				if (tag == params.size()) {
					return ErrorCode.SUCCESS;
				}
			} catch (MstpException e) {
				e.printStackTrace();
				return e.getErrorCode();
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return ErrorCode.ERR_PARAM_ERROR;
		}
		
		return ErrorCode.ERR_COMMONT_ERROR;
	}
	
}
